﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Infinity.Models;
using Infinity.BusinessLayer;
using PagedList;
using PagedList.Mvc;

namespace Infinity.Areas.Managerial.Controllers
{
    public class CategoryController : Controller
    {
        InfinityEntities context = new InfinityEntities();
        //
        // GET: /Managerial/Category/Index
        public ActionResult Index(int? page)
        {

            PagedList.IPagedList<CategoryViewModel> list = context.Categories.Select(c => new CategoryViewModel
            {
                Category_Name = c.Category_Name,

                CategoryID = c.CategoryID
            }).ToList().ToPagedList(page ?? 1, 4);//.ToPagedList(page ?? 1,4);
            return View(list);
        }
        [HttpPost]
        public ActionResult Index(string searchBy, string searchfor, int? page)
        {

            PagedList.IPagedList<CategoryViewModel> list = context.Categories.Select(c => new CategoryViewModel
            {
                Category_Name = c.Category_Name,

                CategoryID = c.CategoryID
            }).ToList().ToPagedList(page ?? 1, 4);

            if (searchBy == "name")
            {
                list = list.Where(i => i.Category_Name.ToLower().Contains(searchfor.ToLower()) || searchfor == null).ToList().ToPagedList(page ?? 1, 4);
                // query = context.Categories.Where(x => x.Category_Name.Contains(searchfor));
            }
            if (searchBy == "id")
            {
                //query = context.Categories.Where(x => x.CategoryID == int.Parse(searchfor));
                list = list.Where(i => i.CategoryID == int.Parse(searchfor) || searchfor == null).ToList().ToPagedList(page ?? 1, 4);
            }

            // return View("CategoryList", list);
            return View(list);
        }
        public ActionResult editeCategory(decimal catID)
        {
            Category cat = context.Categories.Where(c => c.CategoryID == catID).FirstOrDefault();
            CategoryViewModel category = new CategoryViewModel
            {
                CategoryID = cat.CategoryID,
                Category_Name = cat.Category_Name
            };

            return View(category);
        }

        [ValidateAntiForgeryToken]
        public ActionResult UpdateCategory(CategoryViewModel cat)
        {
            if (ModelState.IsValid)
            {
                context.SP_UpdateCategory(cat.CategoryID, cat.Category_Name);
                return View("editeCategory", cat);
            }
            return View("editeCategory", cat);
        }


        public ActionResult DeleteCategory(int catID)
        {
            context.Categories.Remove(context.Categories.Where(c => c.CategoryID == catID).FirstOrDefault());
            context.SaveChanges();
            List<CategoryViewModel> list = context.Categories.Select(c => new CategoryViewModel
            {
                Category_Name = c.Category_Name,
                CategoryID = c.CategoryID
            }).ToList();
            return View("CategoryList", list);
        }




        public ActionResult NewCategory()
        {

            CategoryViewModel cat = new CategoryViewModel();

            cat.CategoryID = DateTime.Now.Ticks;
            return View(cat);


        }
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult NewCategory(CategoryViewModel cat)
        {
            if (ModelState.IsValid)
            {
                Category c = new Category();
                c.Category_Name = cat.Category_Name;
                c.CategoryID = cat.CategoryID;

                context.Categories.Add(c);
                context.SaveChanges();
                return RedirectToAction("Index");
            }
            else return View(cat);
        }


    }
}
